1
|
|
|
"use strict"; |
2
|
|
|
|
3
|
1 |
|
module.exports = create; |
4
|
|
|
|
5
|
1 |
|
const cookie = require('cookie'); |
6
|
1 |
|
const queryString = require('query-string'); |
7
|
1 |
|
const merge = require('merge-objects'); |
8
|
|
|
|
9
|
|
|
function create(document, settings) { |
10
|
2 |
|
settings = merge(settings || {}, { |
11
|
|
|
sources: [ |
12
|
|
|
{referrer: 'google'}, |
13
|
|
|
{referrer: 'yahoo'}, |
14
|
|
|
{referrer: 'bing'}, |
15
|
|
|
{queryParameter: 'gclid', 'value': 'adwords'}, |
16
|
|
|
{queryParameter: 'msclkid', 'value': 'bingads'}, |
17
|
|
|
{queryParameter: 'utm_source'} |
18
|
|
|
], |
19
|
|
|
cookie: { |
20
|
|
|
name: 'last_click', |
21
|
|
|
expires: 30 * 24 * 60 * 60 // 30 days in seconds |
22
|
|
|
} |
23
|
|
|
}); |
24
|
|
|
|
25
|
2 |
|
const location = document.location; |
26
|
|
|
|
27
|
2 |
|
const obj = { |
28
|
|
|
getLastClick: getLastClick, |
29
|
|
|
check: check |
30
|
|
|
}; |
31
|
|
|
|
32
|
2 |
|
obj.check(); |
33
|
|
|
|
34
|
2 |
|
return obj; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @param {String} value |
38
|
|
|
*/ |
39
|
|
|
function save(value) { |
40
|
4 |
|
const date = new Date(); |
41
|
4 |
|
date.setTime(date.getTime() + (settings.cookie.expires * 1000)); |
42
|
|
|
|
43
|
4 |
|
document.cookie = cookie.serialize(settings.cookie.name, value, { |
|
|
|
|
44
|
|
|
httpOnly: true, |
45
|
|
|
maxAge: settings.cookie.expires, |
46
|
|
|
expires: date, |
47
|
|
|
path: '/' |
48
|
|
|
}); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @return {String|*} |
53
|
|
|
*/ |
54
|
|
|
function getLastClick() { |
55
|
2 |
|
const cookies = cookie.parse(document.cookie); |
|
|
|
|
56
|
2 |
|
if(hasProperty(cookies, settings.cookie.name)) { |
57
|
2 |
|
return cookies[settings.cookie.name]; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
return null; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
function check() { |
64
|
4 |
|
const queryParams = queryString.parse(location.search); |
65
|
4 |
|
settings.sources.forEach(function (source) { |
66
|
24 |
|
let val = ''; |
67
|
|
|
|
68
|
24 |
|
if(hasProperty(source, 'queryParameter') && hasProperty(queryParams, source.queryParameter)) { |
69
|
2 |
|
val = queryParams[source.queryParameter]; |
70
|
22 |
|
} else if(hasProperty(source, 'referrer') && document.referrer && document.referrer.indexOf(source.referrer) >= 0) { |
|
|
|
|
71
|
2 |
|
val = document.referrer; |
72
|
|
|
} |
73
|
|
|
|
74
|
24 |
|
if(val) { |
75
|
4 |
|
if(hasProperty(source, 'value')) { |
76
|
2 |
|
if(typeof(source.value) === 'function') { |
77
|
|
|
val = source.value.apply(val); |
78
|
|
|
} else { |
79
|
|
|
val = source.value; |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|
83
|
4 |
|
save(val); |
84
|
|
|
} |
85
|
|
|
}); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @param {Object} obj |
90
|
|
|
* @param {String} property |
91
|
|
|
* @return {boolean} |
92
|
|
|
*/ |
93
|
|
|
function hasProperty(obj, property) { |
94
|
64 |
|
return Object.keys(obj).indexOf(property) !== -1 |
95
|
|
|
} |
96
|
|
|
} |